/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.ui.base;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import com.reportgear.report.view.grid.util.Utils;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class JNumberField extends JTextField {

	public static final double ERROR_VALUE = Double.MAX_VALUE;
	private int maxIntegerLength;
	private int maxDecimalLength;
	private double minValue;
	private double maxValue;
	private boolean isContentChanged;

	public JNumberField() {
		this(32, 16);
	}

	public JNumberField(int paramInt) {
		super(paramInt);
		this.maxIntegerLength = 24;
		this.maxDecimalLength = 16;
		this.minValue = Double.MIN_VALUE;
		this.maxValue = Double.MAX_VALUE;
		this.isContentChanged = false;
	}

	public JNumberField(int paramInt1, int paramInt2) {
		this(paramInt1, paramInt2, Double.MIN_VALUE, Double.MAX_VALUE);
	}

	public JNumberField(int paramInt1, int paramInt2, double paramDouble1, double paramDouble2) {
		this.maxIntegerLength = 24;
		this.maxDecimalLength = 16;
		this.minValue = Double.MIN_VALUE;
		this.maxValue = Double.MAX_VALUE;
		this.isContentChanged = false;
		this.maxIntegerLength = paramInt1;
		this.maxDecimalLength = paramInt2;
		this.minValue = paramDouble1;
		this.maxValue = paramDouble2;
		setDocument(new NumberDocument());
	}

	public int getMaxIntegerLength() {
		return this.maxIntegerLength;
	}

	public void setMaxIntegerLength(int paramInt) {
		this.maxIntegerLength = paramInt;
	}

	public int getMaxDecimalLength() {
		return this.maxDecimalLength;
	}

	public void setMaxDecimalLength(int paramInt) {
		this.maxDecimalLength = paramInt;
	}

	public double getMinValue() {
		return this.minValue;
	}

	public void setMinValue(double paramDouble) {
		this.minValue = paramDouble;
	}

	public double getMaxValue() {
		return this.maxValue;
	}

	public void setMaxValue(double paramDouble) {
		this.maxValue = paramDouble;
	}

	public void setValue(double paramDouble) {
		setText(Utils.doubleToString(paramDouble));
	}

	public double getValue() throws NumberFormatException {
		try {
			if (getText().length() == 0) {
				return 0.0D;
			}
			return Double.parseDouble(getText());
		} catch (NumberFormatException ex) {
		}
		return Double.MAX_VALUE;
	}

	public String getTextValue() {
		return getText();
	}

	public void setInteger(boolean paramBoolean) {
		if (paramBoolean) {
			this.maxDecimalLength = 0;
		} else {
			this.maxDecimalLength = 16;
		}
	}

	public boolean isContentChanged() {
		return this.isContentChanged;
	}

	class NumberDocument extends PlainDocument {

		@Override
		public void insertString(int paramInt, String paramString, AttributeSet paramAttributeSet)
				throws BadLocationException {
			String str2;
			String str1 = getText(0, getLength());
			if ((paramString.equals("F")) || (paramString.equals("f")) || (paramString.equals("D"))
					|| (paramString.equals("d"))
					|| ((str1.trim().equals("0")) && (!(paramString.substring(0, 1).equals("."))) && (paramInt != 0))
					|| ((paramString.equals(".")) && (JNumberField.this.maxDecimalLength == 0))) {
				Toolkit.getDefaultToolkit().beep();
				return;
			}
			String str3 = "";
			String str4 = str1.substring(0, paramInt) + paramString + str1.substring(paramInt, getLength());
			str4 = str4.replaceFirst("-", "");
			int i = str4.indexOf(".");
			if (i > -1) {
				str2 = str4.substring(0, i);
				str3 = str4.substring(i + 1);
			} else {
				str2 = str4;
			}
			if ((str2.length() > JNumberField.this.maxIntegerLength)
					|| (str3.length() > JNumberField.this.maxDecimalLength)
					|| ((str4.length() > 1) && (str4.substring(0, 1).equals("0")) && (!(str4.substring(1, 2)
							.equals("."))))) {
				Toolkit.getDefaultToolkit().beep();
				return;
			}
			try {
				if ((!(str4.equals(""))) && (!(str4.equals("-")))) {
					double d = Double.parseDouble(str4);
					if ((d < JNumberField.this.minValue) || (d > JNumberField.this.maxValue)) {
						throw new Exception();
					}
				}
			} catch (Exception localException) {
				Toolkit.getDefaultToolkit().beep();
				return;
			}
			// JNumberField.access$402(JNumberField.this, true);
			super.insertString(paramInt, paramString, paramAttributeSet);
		}
	}

}
